package com.ybkj.daijia.api.driver.v4;

import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.DriverLogin;
import com.ybkj.daijia.server.model.Notice;
import com.ybkj.daijia.server.model.NoticeXDriver;
import com.ybkj.daijia.server.service.DriverLoginService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.NoticeService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("NoticeControllerV4")
@RequestMapping(value = "driver/api/rest/v4")
public class NoticeController {

    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverLoginService driverLoginService;


    /**
     * 查询通知信息
     *
     * @param driverID
     * @param page
     * @param limit
     * @param appKey
     * @param token
     * @param timestamp
     * @return
     */
    @RequestMapping(value = "queryNotice", method = RequestMethod.GET)
    public @ResponseBody
    ResultTransport queryNotice(String employToken, Integer page, Integer limit) {

        logger.debug("recv employToken:{},page:{},limit:{}", employToken, page, limit);

        if (StringUtils.isBlank(employToken) || null == page || null == limit) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }

        try {

            Page<Map<String, Object>> notice = noticeService
                .queryNoticeV3(driverLogin.getDriverId(), page, limit);
            Long notRead = noticeService.countNotRead(driverLogin.getDriverId());

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("notice", notice);
            map.put("notRead", notRead);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(map);

            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver queryNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }

    @RequestMapping(value = "readNotice", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport readNotice(String employToken, Long noticeId) {

        logger.debug("recv employToken:{},noticeId:{}", employToken, noticeId);

        if (StringUtils.isBlank(employToken) || null == noticeId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }

        try {
            NoticeXDriver noticeXDriver = noticeService
                .findByDriverAndNotice(driverLogin.getDriverId(), noticeId);

            if (null != noticeXDriver) {
                noticeService.updateHasRead(noticeXDriver.getId());
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);

            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver readNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "readAllNotice", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport readNotice(String employToken) {

        logger.debug("recv employToken:{}", employToken);

        if (StringUtils.isBlank(employToken)) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }

        try {
            noticeService.readAllNotice(driverLogin.getDriverId());

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);

            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver readAllNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "getNotice", method = RequestMethod.GET)
    public @ResponseBody
    ResultTransport getNotice(Long noticeId) {

        logger.debug("recv noticeId:{}", noticeId);

        if (null == noticeId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        try {

            Notice notice = noticeService.findOne(noticeId);

            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("id", notice.getId());
            dataMap.put("content", notice.getContent());

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(dataMap);

            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver getNotice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }


    /**
     * 查询通知信息
     */
    @RequestMapping("viewNotice/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView mv = new ModelAndView();

        Notice notice = noticeService.findOne(id);

        mv.addObject("notice", notice);

        mv.setViewName("notice/view");

        return mv;
    }
}
